﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 功能测试软件.BLL
{
    class SetViewBLL
    {
        private string iniPath;
        public SetViewBLL()
        {
            iniPath = Environment.CurrentDirectory + "\\设置.ini";
        }
        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="configModel"></param>
        public void SaveConfigModel(Model.ConfigModel configModel)
        {
            try
            {
                BtLabelTool.IniHelper.IniWriteValue("电流", "继电器串口", configModel.RelayPortname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("电流", "采集电流阈值", configModel.Current, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("信号", "蓝牙串口", configModel.BtPortname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("信号", "蓝牙搜索时长", configModel.ScanDelay, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("信号", "蓝牙强度阈值", configModel.dBm, iniPath);

                BtLabelTool.IniHelper.IniWriteValue("通道1", "开启", configModel.Channel1Enable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道1", "串口", configModel.Channel1Portname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道1", "刷图", configModel.Channel1PicEnable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道1", "图片类型", configModel.Channel1PicTemp, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道1", "点灯", configModel.Channel1RgbEnable, iniPath);

                BtLabelTool.IniHelper.IniWriteValue("通道2", "开启", configModel.Channel2Enable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道2", "串口", configModel.Channel2Portname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道2", "刷图", configModel.Channel2PicEnable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道2", "图片类型", configModel.Channel2PicTemp, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道2", "点灯", configModel.Channel2RgbEnable, iniPath);

                BtLabelTool.IniHelper.IniWriteValue("通道3", "开启", configModel.Channel3Enable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道3", "串口", configModel.Channel3Portname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道3", "刷图", configModel.Channel3PicEnable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道3", "图片类型", configModel.Channel3PicTemp, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道3", "点灯", configModel.Channel3RgbEnable, iniPath);

                BtLabelTool.IniHelper.IniWriteValue("通道4", "开启", configModel.Channel4Enable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道4", "串口", configModel.Channel4Portname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道4", "刷图", configModel.Channel4PicEnable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道4", "图片类型", configModel.Channel4PicTemp, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道4", "点灯", configModel.Channel4RgbEnable, iniPath);

                BtLabelTool.IniHelper.IniWriteValue("通道5", "开启", configModel.Channel5Enable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道5", "串口", configModel.Channel5Portname, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道5", "刷图", configModel.Channel5PicEnable, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道5", "图片类型", configModel.Channel5PicTemp, iniPath);
                BtLabelTool.IniHelper.IniWriteValue("通道5", "点灯", configModel.Channel5RgbEnable, iniPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        /// <param name="viewModel"></param>
        public void InitView(ViewModel.SetViewModel viewModel)
        {
            try
            {
                viewModel.Current = View.TestView.config.Current;
                viewModel.RelayPort = View.TestView.config.RelayPortname;

                viewModel.BtPort = View.TestView.config.BtPortname;
                viewModel.ScanDelay = View.TestView.config.ScanDelay;
                viewModel.dBm = View.TestView.config.dBm;

                viewModel.Channel1Enable = View.TestView.config.Channel1Enable == "true" ? true : false;
                viewModel.Channel2Enable = View.TestView.config.Channel2Enable == "true" ? true : false;
                viewModel.Channel3Enable = View.TestView.config.Channel3Enable == "true" ? true : false;
                viewModel.Channel4Enable = View.TestView.config.Channel4Enable == "true" ? true : false;
                viewModel.Channel5Enable = View.TestView.config.Channel5Enable == "true" ? true : false;

                viewModel.Channel1Port = View.TestView.config.Channel1Portname;
                viewModel.Channel2Port = View.TestView.config.Channel2Portname;
                viewModel.Channel3Port = View.TestView.config.Channel3Portname;
                viewModel.Channel4Port = View.TestView.config.Channel4Portname;
                viewModel.Channel5Port = View.TestView.config.Channel5Portname;

                viewModel.Channel1PicEnable = View.TestView.config.Channel1PicEnable == "true" ? true : false;
                viewModel.Channel1isQr = View.TestView.config.Channel1PicTemp == "QR" ? true : false;
                viewModel.Channel1isRedBlack = View.TestView.config.Channel1PicTemp == "BlackRed" ? true : false;

                viewModel.Channel2PicEnable = View.TestView.config.Channel2PicEnable == "true" ? true : false;
                viewModel.Channel2isQr = View.TestView.config.Channel2PicTemp == "QR" ? true : false;
                viewModel.Channel2isRedBlack = View.TestView.config.Channel2PicTemp == "BlackRed" ? true : false;

                viewModel.Channel3PicEnable = View.TestView.config.Channel3PicEnable == "true" ? true : false;
                viewModel.Channel3isQr = View.TestView.config.Channel3PicTemp == "QR" ? true : false;
                viewModel.Channel3isRedBlack = View.TestView.config.Channel3PicTemp == "BlackRed" ? true : false;

                viewModel.Channel4PicEnable = View.TestView.config.Channel4PicEnable == "true" ? true : false;
                viewModel.Channel4isQr = View.TestView.config.Channel4PicTemp == "QR" ? true : false;
                viewModel.Channel4isRedBlack = View.TestView.config.Channel4PicTemp == "BlackRed" ? true : false;

                viewModel.Channel5PicEnable = View.TestView.config.Channel5PicEnable == "true" ? true : false;
                viewModel.Channel5isQr = View.TestView.config.Channel5PicTemp == "QR" ? true : false;
                viewModel.Channel5isRedBlack = View.TestView.config.Channel5PicTemp == "BlackRed" ? true : false;

                viewModel.Channel1RgbEnable = View.TestView.config.Channel1RgbEnable == "true" ? true : false;
                viewModel.Channel2RgbEnable = View.TestView.config.Channel2RgbEnable == "true" ? true : false;
                viewModel.Channel3RgbEnable = View.TestView.config.Channel3RgbEnable == "true" ? true : false;
                viewModel.Channel4RgbEnable = View.TestView.config.Channel4RgbEnable == "true" ? true : false;
                viewModel.Channel5RgbEnable = View.TestView.config.Channel5RgbEnable == "true" ? true : false;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化图片使能透明度
        /// </summary>
        /// <param name="viewModel"></param>
        public void PicViewInit(ViewModel.SetViewModel viewModel)
        {
            try
            {
                viewModel.Channel1PicOpacity = viewModel.Channel1PicEnable == true ? 1 : 0.1;
                viewModel.Channel2PicOpacity = viewModel.Channel2PicEnable == true ? 1 : 0.1;
                viewModel.Channel3PicOpacity = viewModel.Channel3PicEnable == true ? 1 : 0.1;
                viewModel.Channel4PicOpacity = viewModel.Channel4PicEnable == true ? 1 : 0.1;
                viewModel.Channel5PicOpacity = viewModel.Channel5PicEnable == true ? 1 : 0.1;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 初始化串口下拉框
        /// </summary>
        /// <param name="viewModel"></param>
        public void InitPorts(ViewModel.SetViewModel viewModel)
        {
            try
            {
                string[] ports = BtLabelTool.PortHelper.GetPorts();
                foreach (var item in ports)
                {
                    viewModel.Ports.Add(item);
                }
                viewModel.Ports.Add("");
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 更改后赋值给全局变量config
        /// </summary>
        /// <param name="viewModel"></param>
        public void ChangeConfig(ViewModel.SetViewModel viewModel)
        {
            try
            {
                View.TestView.config.Current = viewModel.Current;
                View.TestView.config.RelayPortname = viewModel.RelayPort;

                View.TestView.config.BtPortname = viewModel.BtPort;
                View.TestView.config.ScanDelay = viewModel.ScanDelay;
                View.TestView.config.dBm = viewModel.dBm;

                View.TestView.config.Channel1Enable = viewModel.Channel1Enable == true ? "true" : "false";
                View.TestView.config.Channel2Enable = viewModel.Channel2Enable == true ? "true" : "false";
                View.TestView.config.Channel3Enable = viewModel.Channel3Enable == true ? "true" : "false";
                View.TestView.config.Channel4Enable = viewModel.Channel4Enable == true ? "true" : "false";
                View.TestView.config.Channel5Enable = viewModel.Channel5Enable == true ? "true" : "false";

                View.TestView.config.Channel1Portname = viewModel.Channel1Port;
                View.TestView.config.Channel2Portname = viewModel.Channel2Port;
                View.TestView.config.Channel3Portname = viewModel.Channel3Port;
                View.TestView.config.Channel4Portname = viewModel.Channel4Port;
                View.TestView.config.Channel5Portname = viewModel.Channel5Port;

                View.TestView.config.Channel1PicEnable = viewModel.Channel1PicEnable == true ? "true" : "false";
                View.TestView.config.Channel1PicTemp = viewModel.Channel1isQr == true ? "QR" : "BlackRed";

                View.TestView.config.Channel2PicEnable = viewModel.Channel2PicEnable == true ? "true" : "false";
                View.TestView.config.Channel2PicTemp = viewModel.Channel2isQr == true ? "QR" : "BlackRed";

                View.TestView.config.Channel3PicEnable = viewModel.Channel3PicEnable == true ? "true" : "false";
                View.TestView.config.Channel3PicTemp = viewModel.Channel3isQr == true ? "QR" : "BlackRed";

                View.TestView.config.Channel4PicEnable = viewModel.Channel4PicEnable == true ? "true" : "false";
                View.TestView.config.Channel4PicTemp = viewModel.Channel4isQr == true ? "QR" : "BlackRed";

                View.TestView.config.Channel5PicEnable = viewModel.Channel5PicEnable == true ? "true" : "false";
                View.TestView.config.Channel5PicTemp = viewModel.Channel5isQr == true ? "QR" : "BlackRed";

                View.TestView.config.Channel1RgbEnable = viewModel.Channel1RgbEnable == true ? "true" : "false";
                View.TestView.config.Channel2RgbEnable = viewModel.Channel2RgbEnable == true ? "true" : "false";
                View.TestView.config.Channel3RgbEnable = viewModel.Channel3RgbEnable == true ? "true" : "false";
                View.TestView.config.Channel4RgbEnable = viewModel.Channel4RgbEnable == true ? "true" : "false";
                View.TestView.config.Channel5RgbEnable = viewModel.Channel5RgbEnable == true ? "true" : "false";

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 检查是否有重复串口
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public bool PortCheck(ViewModel.SetViewModel viewModel)
        {
            try
            {
                List<string> port = new List<string>();
                if (port.Contains(viewModel.BtPort) && viewModel.BtPort != "") return false;
                else port.Add(viewModel.BtPort);
                if (port.Contains(viewModel.RelayPort) && viewModel.RelayPort != "") return false;
                else port.Add(viewModel.RelayPort);
                if (port.Contains(viewModel.Channel1Port) && viewModel.Channel1Port != "") return false;
                else port.Add(viewModel.Channel1Port);
                if (port.Contains(viewModel.Channel2Port) && viewModel.Channel2Port != "") return false;
                else port.Add(viewModel.Channel2Port);
                if (port.Contains(viewModel.Channel3Port) && viewModel.Channel3Port != "") return false;
                else port.Add(viewModel.Channel3Port);
                if (port.Contains(viewModel.Channel4Port) && viewModel.Channel4Port != "") return false;
                else port.Add(viewModel.Channel4Port);
                if (port.Contains(viewModel.Channel5Port) && viewModel.Channel5Port != "") return false;
                else port.Add(viewModel.Channel5Port);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
